<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openAddDialog"
          v-hasPermi="['shop:banner:add']"
        >添加
        </el-button>
      </el-col>
      <right-toolbar @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categoryList">
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="分类名称" align="center" prop="categoryName"/>
      <el-table-column label="是否显示" align="center" key="isVisible">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isVisible"
            :active-value='1'
            :inactive-value='0'
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="分类图片" align="center" prop="categoryPic">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.categoryPic"
            :preview-src-list="[scope.row.categoryPic]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openUpdateDialog(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :hide-on-single-page="false"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @size-change="sizeChange"
      @current-change="pageChange"
    />

    <el-dialog
      :visible.sync="open"
      :title="title"
      width="30%" append-to-body
    >
      <el-form :rules="rules" :model="formData" label-width="120px" ref="form">
        <el-row>
          <el-col :span="20">
            <el-form-item label="分类名称" prop="categoryName">
              <el-input v-model="formData.categoryName" placeholder="请输入分类名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="分类简称" prop="shortName">
              <el-input v-model="formData.shortName" placeholder="请输入分类简称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="分类排序" prop="sort" :rules="[
              { required: true, message: '分类排序不能为空'},
              { type: 'number', message: '分类排序必须为数字值'}
            ]">
              <el-input v-model.number="formData.sort" placeholder="请输入分类排序"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="分类图片" prop="categoryPic">
              <picture-manager width="150" height="150" :value="formData.categoryPic">
              </picture-manager>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="handleAddOrUpdate">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import PictureManager from "@/components/ImageUpload/PictureManager";
import * as categoryApi from "@/api/shop/category.js";

export default {
  name: "ShopCategory",
  components: {
    PictureManager
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      showSearch: true,
      total: 0,
      //分类列表数据
      categoryList: [],
      // 遮罩层
      loading: false,
      //是否打开遮罩层
      open: false,
      //弹出框的标题
      title: '',
      //提交或修改时的表单内容
      formData: {
        categoryPic: []
      },
      //表单校验规则
      rules: {
        categoryName: [{required: true, message: "分类名称不能为空", trigger: "blur"}],
        shortName: [{required: true, message: "分类简称不能为空", trigger: "blur"}],
        categoryPic: [{required: true, message: "图片必选", trigger: "blur"}]
      }
    }
  }
  ,
  computed: {},
  created() {
    this.getList();
  }
  ,
  methods: {
    getList() {
      categoryApi.categoryList(this.queryParams).then((res) => {
        this.total = res.total;
        this.categoryList = res.rows
      });
    }
    ,
    handleStatusChange(row) {
      let text = row.isVisible === 1 ? "展示" : "隐藏";
      this.$confirm('确认要' + text + '分类吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return categoryApi.updateOrSave(row);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function () {
        row.isVisible = row.isVisible === 0 ? 1 : 0;
      });
    }
    ,
    sizeChange(val) {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = val
      this.getList()
    }
    ,
    pageChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    }
    ,
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    }
    ,
    handleDelete(row) {
      this.$confirm('确认要删除分类吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return categoryApi.deleteObj(row.categoryId);
      }).then(() => {
        this.msgSuccess("删除成功");
        this.getList();
      }).catch(function () {
      });
    }
    ,
    handleAddOrUpdate() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let temp = Object.assign({}, this.formData);
          let param = Object.assign(temp, {categoryPic: this.formData.categoryPic[0]});
          categoryApi.updateOrSave({
            ...temp
          }).then((res) => {
            this.initParamData();
            this.open = false;
            this.getList();
          });
        } else {
          return false;
        }
      });

    }
    , openUpdateDialog(row) {
      this.title = '修改';
      this.open = true;
      this.formData = Object.assign({}, row);
      Object.assign(this.formData, {
        categoryPic: [this.formData.categoryPic]
      })
    }
    ,
    openAddDialog() {
      this.title = '添加';
      this.open = true;
      this.initParamData()
    }
    ,
    initParamData() {
      this.formData = {
        categoryPic: []
      }
    }
  }
}
;
</script>
<style lang="scss" scoped>

</style>
